package com.buddy.summary.code.algorithm;

import java.util.Arrays;

public class BubbleSort {

    //1.冒泡排序
    private static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                int temp = 0;
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    //2.冒泡排序优化，取消最后一轮的排序
    private static void bubbleSort2(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            boolean isSorted = true;
            for (int j = 0; j < arr.length - 1; j++) {
                int temp = 0;
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    isSorted = false;//如果右元素交换，就不是有序的，标记变为false
                }
            }
            if (isSorted) {
                break;
            }
        }
    }

    //3.冒泡排序优化
    private static void bubbleSort3(int[] arr) {
        //记录最后一次交换的位置
        int lastExchangeIndex = 0;
        //无序数列的边界，每次比较只需要必到这里为止
        int sortBorder = arr.length - 1;
        for (int i = 0; i < arr.length - 1; i++) {
            //有序标记，每一轮的初始化值都是true
            boolean isSorted = true;
            for (int j = 0; j < sortBorder; j++) {
                int temp = 0;
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    isSorted = false;
                    lastExchangeIndex = j;
                }
            }
            sortBorder = lastExchangeIndex;
            if (isSorted) {
                break;
            }
        }
    }

    //4.冒泡排序优化：鸡尾酒排序
    private static void sort(int[] arr) {
        int tmp = 0;
        for (int i = 0; i < arr.length / 2; i++) {
            //有序标记，每一轮的初始值都是true
            boolean isSorted = true;
            //基数轮：从左向右比较和交换
            for (int j = i; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    isSorted = false;
                }
            }
            if (isSorted)
                break;
            isSorted = true;
            //偶数轮
            for (int j = arr.length - i - 1; j > i; j--) {
                if (arr[j] < arr[j-1]){
                    tmp = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = tmp;
                    isSorted = false;
                }
            }
            if (isSorted)
                break;
        }
    }

    public static void main(String[] args) {
        int[] arr1 = new int[]{5,8,6,3,9,2,1,7};
        bubbleSort(arr1);//冒泡排序
        System.out.println(Arrays.toString(arr1));
        int[] arr2 = new int[]{5,8,6,3,9,2,1,7};
        bubbleSort2(arr2);
        System.out.println(Arrays.toString(arr2));
        int[] arr3 = new int[]{3,4,2,1,5,6,7,8};
        bubbleSort3(arr3);
        System.out.println(Arrays.toString(arr3));
        int[] arr4 = new int[]{2,3,4,5,6,7,8,1};
        sort(arr4);
        System.out.println(Arrays.toString(arr4));
    }

}
